package com.sgb.kjwl.aspectj.login;

import android.content.Context;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

/**
 * 登录切面
 */
@Aspect
public class LoginFilterAspect {
    private static final String TAG = "LoginFilterAspect";

    @Pointcut("execution(@com.sgb.kjwl.aspectj.login.LoginCheck * *(..))")
    public void LoginCheck() {
    }

    @Around("LoginCheck()")
    public void aroundLoginPoint(ProceedingJoinPoint joinPoint) throws Throwable {
        ILogin iLogin = LoginAssistant.getInstance().getiLogin();
        if (iLogin == null) {
            throw new NoInitException("LoginSDK 没有初始化！");
        }

        Signature signature = joinPoint.getSignature();
        if (!(signature instanceof MethodSignature)) {
            throw new AnnotationException("LoginCheck 注解只能用于方法上");
        }
        MethodSignature methodSignature = (MethodSignature) signature;
        LoginCheck loginCheck = methodSignature.getMethod().getAnnotation(LoginCheck.class);
        if (loginCheck == null) {
            return;
        }

        Context param = LoginAssistant.getInstance().getApplicationContext();
        if (iLogin.isLogin(param)) {
            if (loginCheck.needRealName()){
                if (iLogin.isReal(param)){
                    joinPoint.proceed();
                }else {
                    iLogin.realName(param,loginCheck.realDefine());
                }
            }else {
                joinPoint.proceed();
            }
        } else {
            iLogin.login(param, loginCheck.userDefine());
        }

    }
}
